
//
//  ExpertModel.m
//  DaJiang
//
//  Created by sally on 16/10/12.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "ExpertModel.h"

@implementation ExpertModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

+ (JSONKeyMapper *)keyMapper {
  return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id" : @"expertid" }];
}

//获取全部专家列表
+ (void)queryAllExpertListWithLocation:(NSString *)location
                                cursor:(NSInteger)cursor
                            completion:(void (^)(NSMutableArray *expertList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          ExpertModel *bannerModel = [[ExpertModel alloc] initWithDictionary:item error:nil];
          [retMArray addObject:bannerModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface queryAllExpertListWithLocation:location cursor:cursor completion:_completion error:_error];
}

//一级分类目录下的全部专家
+ (void)queryLevel1AllExpertListWithLocation:(NSString *)location
                                    proIndex:(NSString *)proIndex
                                      cursor:(NSInteger)cursor
                                  completion:(void (^)(NSMutableArray *expertList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          ExpertModel *bannerModel = [[ExpertModel alloc] initWithDictionary:item error:nil];
          [retMArray addObject:bannerModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface queryLevel1AllExpertListWithLocation:location proIndex:proIndex cursor:cursor completion:_completion error:_error];
}

//二级分类目录下的所有专家
+ (void)queryLevel2AllExpertListWithLocation:(NSString *)location
                                    proIndex:(NSString *)proIndex
                                   proIndex2:(NSString *)proIndex2
                                      cursor:(NSInteger)cursor
                                  completion:(void (^)(NSMutableArray *expertList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          ExpertModel *bannerModel = [[ExpertModel alloc] initWithDictionary:item error:nil];
          [retMArray addObject:bannerModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface queryLevel2AllExpertListWithLocation:location proIndex:proIndex proIndex2:proIndex2 cursor:cursor completion:_completion error:_error];
}

//专家排序
+ (void)sortExpertListWithLocation:(NSString *)location
                          proIndex:(NSString *)proIndex
                         proIndex2:(NSString *)proIndex2
                              page:(NSInteger)page
                              type:(ExpertSort_Type)type
                        completion:(void (^)(NSMutableArray *expertList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          ExpertModel *bannerModel = [[ExpertModel alloc] initWithDictionary:item error:nil];
          [retMArray addObject:bannerModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface sortExpertListWithLocation:location proIndex:proIndex proIndex2:proIndex2 page:page type:type completion:_completion error:_error];
}

//搜索
+ (void)searchExpertListWithLocation:(NSString *)location
                             content:(NSString *)content
                          completion:(void (^)(NSMutableArray *expertList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          ExpertModel *bannerModel = [[ExpertModel alloc] initWithDictionary:item error:nil];
          [retMArray addObject:bannerModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface searchExpertListWithLocation:location content:content completion:_completion error:_error];
}

//约见是否开放
+ (void)setMeetAvailableWithAvailable:(NSInteger)available
                           completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface setMeetAvailableWithAvailable:available completion:_completion error:_error];
}

@end

@implementation RecommendModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

+ (JSONKeyMapper *)keyMapper {
  return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id" : @"recommendid" }];
}

//获取推荐专家列表(首页)
+ (void)queryRecommendListWithLocation:(NSString *)location
                                cursor:(NSInteger)cursor
                            completion:(void (^)(NSMutableArray *recommendList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          RecommendModel *bannerModel = [[RecommendModel alloc] initWithDictionary:item error:nil];
          [retMArray addObject:bannerModel];
        }
      }
      //[ZJModelTool createModelWithDictionary:resultObject[@"list"][0] modelName:@"RecommendModel"];
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface queryRecommendListWithLocation:location cursor:cursor completion:_completion error:_error];
}

@end

@implementation RecommendData

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

@end

@implementation ExpertDetailModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

//专家详情
+ (void)queryExpertDetailWithProUid:(NSInteger)uid
                         completion:(void (^)(ExpertDetailModel *expertDetail))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    ExpertDetailModel *expertDetail = nil;

    if (code == SUCCESS_RESULT) {
      expertDetail = [[ExpertDetailModel alloc] initWithDictionary:resultObject error:nil];
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
    }

    completion(expertDetail);
  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface queryExpertDetailWithProUid:uid completion:_completion error:_error];
}

@end

@implementation CommentModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

+ (JSONKeyMapper *)keyMapper {
  return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id" : @"commentid" }];
}

//查看更多评论
+ (void)queryCommentListWithProUid:(NSInteger)proUid
                            cursor:(NSInteger)cursor
                        completion:(void (^)(NSMutableArray *commentList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          CommentModel *commentModel = [[CommentModel alloc] initWithDictionary:item error:nil];
          [retMArray addObject:commentModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface queryCommentListWithProUid:proUid cursor:cursor completion:_completion error:_error];
}

@end

@implementation CommentData

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

@end

@implementation CollectModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

+ (JSONKeyMapper *)keyMapper {
  return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id" : @"collectid" }];
}

//我的心愿单
+ (void)queryCollectListWithCursor:(NSInteger)cursor
                        completion:(void (^)(NSMutableArray *collectList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          CollectModel *collectModel = [[CollectModel alloc] initWithDictionary:item error:nil];
          [retMArray addObject:collectModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface queryCollectListWithCursor:cursor completion:_completion error:_error];
}

@end

@implementation CollectData

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

@end

@implementation FreeModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

+ (JSONKeyMapper *)keyMapper {
  return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id" : @"freeid" }];
}

//专家有空列表
+ (void)queryFreeListWithCompletion:(void (^)(NSMutableArray *freeList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          FreeModel *freeModel = [[FreeModel alloc] initWithDictionary:item error:nil];
          [self setFreeStateDesc:freeModel];
          [retMArray addObject:freeModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface queryFreeListWithCompletion:_completion error:_error];
}

//添加有空
+ (void)addFreeWithTime:(NSString *)time
               location:(NSString *)location
             completion:(void (^)(FreeModel *freeModel))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    FreeModel *freeModel = nil;

    if (code == SUCCESS_RESULT) {
      freeModel = [[FreeModel alloc] initWithDictionary:resultObject error:nil];
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
    }

    completion(freeModel);
  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface addFreeWithTime:time location:location completion:_completion error:_error];
}

//专家可约的有空列表
+ (void)queryExpertFreeListWithProUid:(NSInteger)proUid
                           completion:(void (^)(NSMutableArray *freeList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          FreeModel *freeModel = [[FreeModel alloc] initWithDictionary:item error:nil];
          [self setFreeStateDesc:freeModel];
          [retMArray addObject:freeModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface queryExpertFreeListWithProUid:proUid completion:_completion error:_error];
}

//装配订单状态描述
+ (void)setFreeStateDesc:(FreeModel *)freeModel {
  NSInteger state = freeModel.data.state;
  //TODO: maybe文案要替换
  switch (state) {
    case 0:
      freeModel.data.state_desc = @"空闲";
      break;
    case 1:
      freeModel.data.state_desc = @"进行中，待定";
      break;
    case 2:
      freeModel.data.state_desc = @"进行中，有约";
      break;
    case 3:
      freeModel.data.state_desc = @"结束";
      break;
    default:
      break;
  }
}

@end

@implementation FreeData

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

@end

@implementation BecomeExpertModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

//初始化
- (instancetype)initWithNull {
  self = [super init];
  if (self) {
    UserModel *userModel = [[CacheManager shareInstance] getLastLoginAccount];
    self.uid = userModel.uid;
    self.education = @"";
    self.loc = @"";
    self.witness = @"";
    self.idCardPic = @"";
    self.qualificationFile = @"";
    self.selfDesc = @"";
    self.userName = @"";
    self.company = @"";
    self.userTitle = @"";
    self.experience = @"";
    self.topicName = @"";
    self.timeLength = @"";
    self.topicDesc = @"";
    self.topicPrice = 0;
    self.bgPic = @"";
    self.topicIndex = @"";
    self.topicIndex2 = @"";
    self.userEmail = @"";
  }
  return self;
}

//申请成为专家
+ (void)applyExpertWithModel:(BecomeExpertModel *)becomeExpertModel
                  completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface applyExpertWithModel:becomeExpertModel completion:_completion error:_error];
}

@end

@implementation ApplyStatusModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

+ (JSONKeyMapper *)keyMapper {
  return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id" : @"statusid" }];
}

//查询申请状态
+ (void)queryApplyStatusWithCompletion:(void (^)(ApplyStatusModel *applyStatusModel))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    ApplyStatusModel *applyStatusModel = nil;

    if (code == SUCCESS_RESULT) {
      NSDictionary *listResultDic = resultObject[DicKeyList];
      NSMutableArray *resultArr = (NSMutableArray *) listResultDic;
      if (resultArr.count > 0) {
        applyStatusModel = [[ApplyStatusModel alloc] initWithDictionary:resultArr[0] error:nil];
      }
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
    }

    completion(applyStatusModel);
  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface queryApplyStatusWithCompletion:_completion error:_error];
}

@end

@implementation ApplyStatusData

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

@end
